package zl.socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

//客户端
public class Client {
	private static Socket client;

	public static void main(String[] args) throws Exception {
		// 通过服务器所在主机IP地址和所监听的主机端口号获得客户端实例
		client = new Socket(InetAddress.getLocalHost(), 8000);
		System.out.println("成功建立连接");
		// 获得客户端socket的输入输出对象。输出对象通过包装避免乱码及手动flush
		PrintStream out = new PrintStream(client.getOutputStream(), true);
		BufferedReader in = new BufferedReader(new InputStreamReader(
				client.getInputStream()));
		while (true) {
			// 循环获取屏幕输入、并发送到服务器
			Scanner input = new Scanner(System.in);
			String str = input.next();
			out.println(str);
			System.out.println(in.readLine());
			// 若输入bye则退出通信
			if ("bye".equalsIgnoreCase(str))
				break;
		}
		in.close();
		out.close();
	}
}
